package client.view;


import client.FriendListManager;
import client.service.RegUser;
import client.service.UpdateFriendListConnector;
import model.Message;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
//密码为空不报错！！！！！
public class Register extends Application {
    private ImageView jlb_north;//北部背景图片标签
    private Button btn_min;
    private Button btn_exit;
    private Label namelabel;
    private Label pwdlabel;
    private TextField qqName;//账号输入框
    private PasswordField qqPwd;//密码输入框
    private Button btn_reg;//南部登录按钮
    private double xOffset = 0;
    private double yOffset = 0;
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();
        primaryStage.initStyle(StageStyle.UNDECORATED);
        // 处理北部背景图片标签
        Image northImg = new Image("file:image/login/login.jpg", 430, 182, false, false);
        jlb_north = new ImageView(northImg);
        root.getChildren().add(jlb_north);
        AnchorPane.setTopAnchor(jlb_north, 0.0);//设置相对位置
        AnchorPane.setLeftAnchor(jlb_north, 0.0);

        btn_min = new Button();
        ImageView minView = new ImageView(new Image("file:image/login/min.jpg"));
        btn_min.setGraphic(minView);
        AnchorPane.setTopAnchor(btn_min, 0.0);
        AnchorPane.setLeftAnchor(btn_min, 360.0);
        btn_min.setStyle("-fx-background-color: transparent;"); // 图片背景透明
        btn_min.setOnAction(e -> primaryStage.setIconified(true));
        root.getChildren().add(btn_min);

        btn_exit = new Button();
        ImageView exitView = new ImageView(new Image("file:image/login/exit.png"));
        btn_exit.setGraphic(exitView);
        AnchorPane.setTopAnchor(btn_exit, 0.0);
        AnchorPane.setLeftAnchor(btn_exit, 390.0);
        btn_exit.setStyle("-fx-background-color: transparent;");
        btn_exit.setOnAction(e -> System.exit(0));
        root.getChildren().add(btn_exit);

        namelabel = new Label("用户名");
        AnchorPane.setTopAnchor(namelabel, 200.0);
        AnchorPane.setLeftAnchor(namelabel, 80.0);
        root.getChildren().add(namelabel);

        //处理用户名输入框
        qqName = new TextField();
        AnchorPane.setTopAnchor(qqName, 195.0);
        AnchorPane.setLeftAnchor(qqName, 120.0);
        qqName.setPrefWidth(194.0);
        qqName.setPrefHeight(30.0);
        root.getChildren().add(qqName);

        pwdlabel=new Label("密码");
        AnchorPane.setTopAnchor(pwdlabel, 245.0);
        AnchorPane.setLeftAnchor(pwdlabel, 80.0);
        root.getChildren().add(pwdlabel);

        //处理密码输入框
        qqPwd = new PasswordField();
        AnchorPane.setTopAnchor(qqPwd, 240.0);
        AnchorPane.setLeftAnchor(qqPwd, 120.0);
        qqPwd.setPrefWidth(194.0);
        qqPwd.setPrefHeight(30.0);
        root.getChildren().add(qqPwd);
        //处理南部注册按钮
        Image RegBtnImg = new Image("file:image/login/regbutton.jpg");
        btn_reg = new Button();
        btn_reg.setGraphic(new ImageView(RegBtnImg));
        btn_reg.setOnAction(e->{
            if(e.getSource() == btn_reg){//点击登录
                String uname = qqName.getText().trim();//获取输入昵称
                String pwd = new String(qqPwd.getText());//获取密码
                Message msg = new RegUser().sendRegInfoToServer(uname , pwd);
                if(null != msg){                  
                	FriendList fl = new FriendList(uname, String.valueOf(msg.getUserID()), msg);
                	FriendListManager.addFriendListFrame(msg.getUserID(), fl);
                	UpdateFriendListConnector upconn = new UpdateFriendListConnector(msg.getUserID());
                    primaryStage.close();
                }
            }
        });
        AnchorPane.setTopAnchor(btn_reg, 299.0);
        AnchorPane.setLeftAnchor(btn_reg, 120.0);
        btn_reg.setPrefWidth(194.0);
        btn_reg.setPrefHeight(30.0);
        btn_reg.setStyle("-fx-background-color: transparent;");
        root.getChildren().add(btn_reg);

        // 添加鼠标按下事件监听器
        root.setOnMousePressed(event -> {
            xOffset = event.getSceneX();
            yOffset = event.getSceneY();
        });

        // 添加鼠标拖拽事件监听器
        root.setOnMouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - xOffset);
            primaryStage.setY(event.getScreenY() - yOffset);
        });

        Scene scene = new Scene(root, 430, 345);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}